﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BonsaiScrum.Mvc.Models;
using BonsaiScrum.Core.Domain.Shared;
using BonsaiScrum.Core.Domain;
using BonsaiScrum.Core.Command;
using BonsaiScrum.Bus;

namespace BonsaiScrum.Mvc.Controllers
{
    public class TaskController : Controller
    {
        private readonly ICommandBus _commandBus;

        public TaskController() { }

        public TaskController(ICommandBus commandBus)
        {
            _commandBus = commandBus;
        }
        
        public ActionResult Index(int storyId)
        {

            return View(storyId);
        }

        public ActionResult Create(int projectId, int storyId)
        {
            var model = new TaskEditModel
            {
                ProjectID = projectId,
                StoryID = storyId,
                Priorities = new SelectList(Enumeration.GetAll<Priority>())
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(TaskEditModel model)
        {
            if (ModelState.IsValid)
            {
                var createTaskCommand = new CreateTask(model.ProjectID, model.StoryID, model.Description, Enumeration.FromName<Priority>(model.Priority), 1);
                _commandBus.Publish(createTaskCommand);

                return View("CreateTaskSuccess", model.StoryID);
            }

            model.Priorities = new SelectList(Enumeration.GetAll<Priority>());
            return View(model);
        }
    }
}
